import com.sun.javafx.collections.ListListenerHelper;

import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-06-15
 * Time: 6:51
 */
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                ", next=" + next +
                '}';
    }
}
public class Test {
    public ListNode middleNode(ListNode head) {
        int count = 1;
        ListNode ans = head;
        while(head.next != null){
            head = head.next;
            count++;
        }
        int ret = count/2;
        while(ret != 0){
            ans = ans.next;
            ret--;
        }
        if(count % 2 == 0){
            return ans.next;
        }
        return ans;
    }

    public static void main(String[] args) {
        List list = new LinkedList();
        list.add(1);
        int a = list.size();
        list.clear();
        a = list.size();
        System.out.println(a);
    }
    public static void main1(String[] args) {
        ListNode head = new ListNode(1);
        ListNode next = new ListNode(2,null);
        head.next = next;
        System.out.println(head.toString());
        next.val = 0;
        next = next.next;

        System.out.println(head.toString());
        head.next = null;
        System.out.println(head.toString());

    }
}
